import logging

import pytest

from resource.pom.settings import Settings
from resource.pom.system_ui import SystemUI
from utils.manager import Images


class Test:
    system_ui = SystemUI
    settings = Settings

    @pytest.mark.parametrize('setup_teardown', [settings.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，右上角下划进入控制中心，点击控制中心飞行模式按钮')
        device.start_ability(self.settings.bundle_name, self.settings.ability_name)
        device.click(*self.settings.mobile_network_item)
        device.refresh_layout()
        airplane_toggle = device.get_element_by_type('Toggle')
        if airplane_toggle.get('checked') == 'true':
            device.click_element(airplane_toggle)
            device.wait(2)
        device.dropdown_controller_center()
        device.refresh_layout()
        airplane_switch = device.get_element_by_text('飞行模式')
        sx, sy = device.center_of_element(airplane_switch)
        device.click(sx, sy - 80)
        logging.info('预期结果1.通知状态栏显示为飞行模式状态，且设置中的飞行开关打开')
        device.wait(5)
        device.swipe_up_dropdown_pan()
        device.refresh_layout()
        airplane_toggle = device.get_element_by_type('Toggle')
        assert airplane_toggle.get('checked') == 'true', '设置中开关未打开'

        status_bar_pic = device.save_snapshot_to_local('statusbar.jpeg')
        status_bar_pic = Images.crop_picture(status_bar_pic, *self.system_ui.statusbar_area)
        is_in = Images.is_image_in_image(self.system_ui.air_mode_icon, status_bar_pic)
        assert is_in, '状态栏没有飞行模式图标'
